#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <time.h>
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define IPSTR "127.0.0.1"                                               
#define PORT 5000                                                              
#define BUFSIZE 4096

int post(const char* host, int port, const char* data, const char* url)
{
    int sockfd, ret;
    struct sockaddr_in servaddr;
    char str1[4096], str2[4096], *str;
    socklen_t len;

    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
        printf("Fail to create connection---socket error!\n");
        exit(0);
    };

    bzero(&servaddr,sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    // servaddr.sin_port = htons(PORT);
    servaddr.sin_port = htons(port);
    //if(inet_pton(AF_INET,  IPSTR, &servaddr.sin_addr) <= 0 ){
    if(inet_pton(AF_INET, host, &servaddr.sin_addr) <= 0 ){
        printf("Fail to create connection---inet_pton error!\n");
        exit(0);
    };

    if(connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0){
        printf("Connect Failed,connect error!\n");
        exit(0);
    }
    printf("Connect Succeed!\n");

    memset(str2, 0, 4096);
    // strcat(str2,"map_name=MapExploer1111");                                     //Input the content of PUT
    strcat(str2, data);
    str=(char*)malloc(128);
    len =strlen(str2);
    sprintf(str,"%d", len);

    memset(str1, 0, 4096);
    // strcat(str1,"PUT /maps/1 HTTP/1.1\n");                                          //Input the type of HTTP and PUT URI
    strcat(str1,"PUT ");
    strcat(str1, url);
    strcat(str1, "\n");
    // strcat(str1,"Host: 127.0.0.1:5000 \n");                                            //Input the Host IP & Port
    strcat(str1,"Host: ");
    strcat(str1, host);
    strcat(str1, " \n");
    strcat(str1,"Content-Type: application/json\n");
    strcat(str1,"Content-Length: ");
    strcat(str1, str);
    strcat(str1,"\n\n");

    strcat(str1, str2);
    strcat(str1,"\r\n\r\n");
    printf("%s\n",str1);

    ret = write(sockfd,str1,strlen(str1));

    return sockfd;
}

int main()
{
    char data[] = "{\"map_name\":\"Exploer1\"}";
    int sockfd = post(IPSTR, PORT, data, "/maps/1 HTTP/1.1");

    fd_set   t_set1;
    FD_ZERO(&t_set1);
    FD_SET(sockfd, &t_set1);

    int h, i;
    struct timeval  tv;
    char buf[BUFSIZE];
    while(1){
        sleep(2);
        tv.tv_sec= 0;
        tv.tv_usec= 0;
        h= 0;
        printf("--------------->1");
        h= select(sockfd +1, &t_set1, NULL, NULL, &tv);
        printf("--------------->2");

        //if (h == 0) continue;
        if(h < 0) {
            close(sockfd);
            printf("Except appears when reading the data , the thread is stopped!\n");
            return -1;
        };

        if(h > 0){
            memset(buf, 0, 4096);
            i= read(sockfd, buf, 4095);
            if(i==0){
                close(sockfd);
                printf("The distance is closed when reading the data , the thread is stopped!\n");
                return -1;
            }

            printf("%s\n", buf);
        }
    }
    close(sockfd);
}
